package com.yml.mall.product;

import com.yml.mall.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Min;

/**
 * @ClassName ProductCotroller
 * @Author Administrator
 * @Date 2020/4/10   10:07
 */

@RestController
@Validated
@Slf4j
public class ProductController {

    private ProductService productService;
    public ProductController(ProductServiceImpl productService){
        this.productService=productService;
    }

    @GetMapping("/AllProductPage")
    public ResponseVO findProductList(@RequestParam(required = false,value = "page" ,defaultValue = "1") @Min(1) @Valid Integer page,
                                      @RequestParam(required = false,value = "size",defaultValue = "10") @Min(0) @Valid Integer size ){

        //if(bindingResult.hasErrors()) return  ResponseVO.error(ResponseEnum.FIELD_ERROR);
        return  productService.findProductListFromAll(page, size);
    }

    @GetMapping("/products")
    public ResponseVO findProductList(@RequestParam(required = false,value = "categoryId",defaultValue = "0") @Min(0) @Valid  Integer categoryId,
                                      @RequestParam(required = false,value = "page" ,defaultValue = "1") @Min(1) @Valid int page,
                                      @RequestParam(required = false,value = "size",defaultValue = "10")  @Min(0) @Valid int size){

        return productService.findProductListFromParent(categoryId,page,size);
    }
}
